<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    :close-on-press-escape="true"
    @close="closeDialog"
    :destroy-on-close="true">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="100px">
      <el-form-item label="栏目名" prop="columnName">
        <el-input v-model="dataForm.columnName" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="父分类" prop="parentId">
        <!--      <el-input v-model="dataForm.parentId" placeholder=""></el-input>-->
        <el-cascader
          v-model="dataForm.parentId"
          :options="menuForm"
          :props="props"
          :show-all-levels="false"
          @change="changeMenuParent(dataForm.parentId)"></el-cascader>
      </el-form-item>
      <el-form-item label="栏目logo" prop="columnLogo" width="200" height="200">
        <el-upload
          v-model="dataForm.columnLogo"
          class="upload-image"
          action="http://localhost:8080/upload/images"
          :show-file-list="false"
          :on-success="handleAvatarSuccess">
          <img v-if="imageUrl" :src="imageUrl" class="avatar-logo">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="界面展示" prop="showPic">
        <!--      <el-input v-model="dataForm.showPic" placeholder=""></el-input>-->
        <el-upload
          v-model="dataForm.showPic"
          class="upload-showPic"
          action="http://localhost:8080/upload/images"
          :show-file-list="false"
          :on-success="handleAvatarSuccess">
          <img v-if="showPicUrl" :src="showPicUrl" class="avatar-showPic">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="栏目官网" prop="columnWeb">
        <el-input v-model="dataForm.columnWeb" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="官方介绍" prop="webIntroduce">
        <el-input v-model="dataForm.webIntroduce" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="栏目类型" prop="columnType">
        <el-input v-model="dataForm.columnType" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="授权信息" prop="license">
        <el-input v-model="dataForm.license" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="下载次数" prop="downloads">
        <el-input v-model="dataForm.downloads" placeholder="0"></el-input>
      </el-form-item>

    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
    export default {
        data() {
            return {
                visible: false,
                menuForm: [],
                props: {
                    label: "menuName",
                    value: "menuId",
                    children: "childs"
                },
                dataForm: {
                    columnId: 0,
                    columnName: '',
                    parentId: [],
                    columnLogo: '',
                    columnWeb: '',
                    webIntroduce: '',
                    columnType: '',
                    license: '',
                    introduceId: '',
                    downloads: 0,
                    showPic: '',
                },
                imageUrl: '',
                showPicUrl: '',
                dataRule: {
                    columnName: [
                        {required: true, message: '不能为空', trigger: 'blur'}
                    ],
                    parentId: [
                        {required: true, message: '不能为空', trigger: 'blur'}
                    ],
                    columnLogo: [
                        {required: true, message: '不能为空', trigger: 'blur'}
                    ],
                    columnWeb: [
                        {required: true, message: '不能为空', trigger: 'blur'}
                    ],
                    webIntroduce: [
                        {required: true, message: '不能为空', trigger: 'blur'}
                    ],
                    columnType: [
                        {required: true, message: '不能为空', trigger: 'blur'}
                    ],
                    license: [
                        {required: true, message: '不能为空', trigger: 'blur'}
                    ],
                    showPic: [
                        {required: true, message: '不能为空', trigger: 'blur'}
                    ]
                }
            }
        },
        methods: {
            closeDialog(){
                this.imageUrl = null
                this.showPicUrl = null
            },
            changeMenuParent(data) {
                console.log(data)
            },
            getMenuList() {
                this.$http({
                    url: this.$http.adornUrl('/generator/column/listTree'),
                    method: 'get'
                }).then(({data}) => {
                    console.log(data.data)
                    this.menuForm = data.data
                })
            },
            handleAvatarSuccess(res) {
                console.log("res:", res)
                this.imageUrl = res.data.fileUrl
            },
            init(id) {
                this.getMenuList()
                this.dataForm.columnId = id || 0
                this.visible = true
                this.$nextTick(() => {
                    this.$refs['dataForm'].resetFields()
                    if (this.dataForm.columnId) {
                        this.$http({
                            url: this.$http.adornUrl(`/generator/column/columnInfo/${this.dataForm.columnId}`),
                            method: 'get',
                            params: this.$http.adornParams()
                        }).then(({data}) => {
                            if (data && data.code === 0) {
                                this.dataForm.columnName = data.column.columnName
                                this.dataForm.parentId = data.column.parentId
                                this.imageUrl = data.column.columnLogo
                                this.dataForm.columnLogo = data.column.columnLogo
                                this.dataForm.columnWeb = data.column.columnWeb
                                this.dataForm.webIntroduce = data.column.webIntroduce
                                this.dataForm.columnType = data.column.columnType
                                this.dataForm.license = data.column.license
                                this.dataForm.introduceId = data.column.introduceId
                                this.dataForm.downloads = data.column.downloads
                                this.dataForm.showPic = data.column.showPic
                                this.showPicUrl = data.column.showPic
                            }
                        })
                    }
                })
            },
            // 表单提交
            dataFormSubmit() {
                this.$refs['dataForm'].validate((valid) => {
                    if (valid) {
                        this.$http({
                            url: this.$http.adornUrl(`/generator/column/${!this.dataForm.columnId ? 'save' : 'updateColumn'}`),
                            method: 'post',
                            data: this.$http.adornData({
                                'columnId': this.dataForm.columnId || undefined,
                                'columnName': this.dataForm.columnName,
                                'parentId': this.dataForm.parentId,
                                'columnLogo': this.dataForm.columnLogo,
                                'columnWeb': this.dataForm.columnWeb,
                                'webIntroduce': this.dataForm.webIntroduce,
                                'columnType': this.dataForm.columnType,
                                'license': this.dataForm.license,
                                'introduceId': this.dataForm.introduceId,
                                'downloads': this.dataForm.downloads,
                                'showPic': this.dataForm.showPic,
                            })
                        }).then(({data}) => {
                            if (data && data.code === 0) {
                                this.$message({
                                    message: '操作成功',
                                    type: 'success',
                                    duration: 1500,
                                    onClose: () => {
                                        this.visible = false
                                        this.$emit('refreshDataList')
                                    }
                                })
                            } else {
                                this.$message.error(data.msg)
                            }
                        })
                    }
                })
            }
        }
    }
</script>

<style>
  .upload-image .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .upload-showPic .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .upload-showPic .el-upload:hover {
    border-color: #409EFF;
  }
  .upload-image .el-upload:hover {
    border-color: #409EFF;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }

  .avatar-logo {
    width: 178px;
    height: 178px;
    display: block;
  }

  .avatar-showPic {
    width: 300px;
    height: 178px;
    display: block;
  }
</style>
